<?php

namespace common\models\project;

use Yii;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "hxdc_project_article".
 *
 * @property int $id
 * @property int $merchant_id 商户id
 * @property int $project_id 项目id
 * @property int $keywords 关键词
 * @property string $title 标题
 * @property string $img_url 图片
 * @property string $content 内容
 *
 * @property int $client_type 客户端类型 0:通用；1:仅PC端；2:手机端通用；3:仅微信端；4:仅抖音端
 * @property string $url_wx_to 微信端跳转链接
 * @property string $url_pc_to PC端跳转链接
 * @property string $other_appid 外部小程序appid
 * @property string $other_wx_url_to 跳转外部微信小程序跳转链接
 *
 * @property int $created_at 创建时间
 * @property int $updated_at 最后一次更新时间
 * @property int $status 状态[-1:删除;0:禁用;1启用]
 * @property int $sort 排序
 */
class ProjectArticle extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'hxdc_project_article';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['merchant_id','client_type', 'project_id', 'created_at', 'updated_at', 'status','sort'], 'integer'],
            [['project_id','client_type', 'title'], 'required'],
            [['title','other_appid'], 'string', 'max' => 63],
            [['url_wx_to', 'url_pc_to', 'other_wx_url_to','img_url','keywords'], 'string', 'max' => 255],
            [['content'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'merchant_id' => '商户 ID',
            'project_id' => '项目 ID',
            'title' => '标题',
            'img_url' => '图片',
            'content' => '内容',
            'client_type' => '客户端显示类型',
            'url_wx_to' => '微信端跳转链接',
            'url_pc_to' => 'PC端跳转链接',
            'other_wx_url_to' => '跳转外部微信小程序跳转链接',
            'other_appid' => '外部小程序appid',
            'keywords' => '关键词',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'status' => '状态',
            'sort' => '排序',
        ];
    }
    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }

        return parent::beforeSave($insert);
    }
    /**
     * @return array
     */
    public function behaviors()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class' => BlameableBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['merchant_id'],
                ],
                'value' => !empty($merchant_id) ? $merchant_id : 0,
            ]
        ];
    }
}
